package org.zanateh.wargame.graphics;

import org.anddev.andengine.opengl.util.FastFloatBuffer;
import org.anddev.andengine.opengl.vertex.VertexBuffer;

/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 * 
 * @author Nicolas Gramlich
 * @since 13:07:25 - 13.03.2010
 */
public class HexagonVertexBuffer extends VertexBuffer {

	
	// ===========================================================
	// Constants
	// ===========================================================

	final private static int[] mIndexConvert = 
		{ 0, 1, 
		  4, 5,
		  8, 9,
		  10, 11,
		  6, 7,
		  2, 3
	};
	
	public static final int VERTICES_PER_HEXAGON = 6;

	private static final int FLOAT_TO_RAW_INT_BITS_ZERO = Float.floatToRawIntBits(0);

	// ===========================================================
	// Fields
	// ===========================================================	
	
	// ===========================================================
	// Constructors
	// ===========================================================

	public HexagonVertexBuffer(final int pDrawType, final boolean pManaged) {
		super(2 * VERTICES_PER_HEXAGON, pDrawType, pManaged);
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================

	public synchronized void update(final float sideLength) {
		float scaleFactor = 0.98f;
		
		float c = sideLength * scaleFactor;
		float a = (c/2) * scaleFactor;
		float b = (float) (Math.sin(Math.toRadians(60))*c) * scaleFactor;

//		final int x = FLOAT_TO_RAW_INT_BITS_ZERO;
//		final int y = FLOAT_TO_RAW_INT_BITS_ZERO;
//		final int x2 = Float.floatToRawIntBits(pWidth);
//		final int y2 = Float.floatToRawIntBits(pHeight);

 
		
		
		final int[] bufferData = this.mBufferData;

		int i = -1;
		
		bufferData[++i] = Float.floatToRawIntBits(-c);    // far left
		bufferData[++i] = FLOAT_TO_RAW_INT_BITS_ZERO;
		
		bufferData[++i] = Float.floatToRawIntBits(-a);     // lower left
		bufferData[++i] = Float.floatToRawIntBits(b);

		bufferData[++i] = Float.floatToRawIntBits(-a);     // upper left
		bufferData[++i] = Float.floatToRawIntBits(-b);

		bufferData[++i] = Float.floatToRawIntBits(a);      // lower right
		bufferData[++i] = Float.floatToRawIntBits(b);
		
		bufferData[++i] = Float.floatToRawIntBits(a);      // upper right
		bufferData[++i] = Float.floatToRawIntBits(-b);
		
		bufferData[++i] = Float.floatToRawIntBits(c);      // far right
		bufferData[++i] = FLOAT_TO_RAW_INT_BITS_ZERO;

		final FastFloatBuffer buffer = this.getFloatBuffer();
		buffer.position(0);
		buffer.put(bufferData);
		buffer.position(0);

		super.setHardwareBufferNeedsUpdate();
	}

	// conversion function to get float coords when necessary (for colldect) and in the right order.
	public float lookup(int index)
	{
		int bufferIndex = mIndexConvert[index];
		
		final int[] bufferData = this.mBufferData;
		return Float.intBitsToFloat(bufferData[bufferIndex]);
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
